package com.lkm.app_ui.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.google.zxing.Result;
import com.google.zxing.client.android.CaptureActivity;
import com.lkm.app_ui.R;
import com.lkm.app_ui.help.OnSteelyardKeyEventHelp;
import com.lkm.comlib.help.ViewHelp;
import com.lkm.comlib.help.ViewUIHelp;

public class CaptureBarcodeActivity extends CaptureActivity {

	public static CaptureBarcodeActivity captureBarcodeActivity ;

	private Handler mainHandler;
	@Override
    public void onCreate(Bundle icicle) {
		captureBarcodeActivity = this;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(icicle);
		mainHandler = new Handler(Looper.getMainLooper());

		ViewGroup viewGroup = getViewGroup(this);
		TextView textView = new TextView(this);
		textView.setText("9,返回");
//		textView.setBounds(0, 0, textView.getMinimumWidth(), textView.getMinimumHeight());
//		textView.setCompoundDrawables(getResources().getDrawable(R.drawable.btn_back), null, null, null);
		
		textView.setTextSize(18.0f);
		textView.setTextColor(Color.WHITE);
		textView.setPadding(30, 20, 20, 20);
		textView.setClickable(true);
		viewGroup.addView(textView);

		textView.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				onBackPressed();
			}
		});

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				View view = findViewById(R.id.preview_view);
				ViewHelp.setViewWidth(view,view.getHeight());
			}
		},1000);

	}
	
	private static ViewGroup getViewGroup(Activity context)
    {  
        return (ViewGroup)context.findViewById(android.R.id.content);
    }

	@Override
	public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
		super.handleDecode(rawResult, barcode, scaleFactor);

		String s = rawResult.getText();

		setResult(RESULT_OK,new Intent().putExtra("data",s));

		finish();

	}

	@Override
	public void handleDecodeXX(Result rawResult, Bitmap barcode, float scaleFactor) {
		super.handleDecodeXX(rawResult, barcode, scaleFactor);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (captureBarcodeActivity == this) captureBarcodeActivity = null;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		OnSteelyardKeyEventHelp.onSendKeyFormK(keyCode,mainHandler);

		return super.onKeyDown(keyCode, event);
	}
}
